package com.turnengine.client.global.user.bean;

import com.javabi.common.locale.LanguageCountry;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The User.
 */
public class User implements IUser, Comparable<IUser> {

	/** The id. */
	private int id = -1;
	/** The name. */
	private String name = "";
	/** The email. */
	private String email = "";
	/** The language. */
	private LanguageCountry language = LanguageCountry.ENGLISH_UNITED_KINGDOM;

	/**
	 * Creates a new User.
	 */
	public User() {
	}

	/**
	 * Creates a new User.
	 * @param id the id
	 * @param name the name
	 * @param email the email
	 * @param language the language
	 */
	public User(int id, String name, String email, LanguageCountry language) {
		setId(id);
		setName(name);
		setEmail(email);
		setLanguage(language);
	}

	/**
	 * Creates a new User.
	 * @param user the user
	 */
	public User(User user) {
		setId(user.getId());
		setName(user.getName());
		setEmail(user.getEmail());
		setLanguage(user.getLanguage());
	}

	/**
	 * Creates a new User.
	 * @param iUser the i user
	 */
	public User(IUser iUser) {
		setId(iUser.getId());
		setName(iUser.getName());
		setEmail(iUser.getEmail());
		setLanguage(iUser.getLanguage());
	}

	/**
	 * Returns the id.
	 * @return the id.
	 */
	public int getId() {
		return id;
	}

	/**
	 * Returns the name.
	 * @return the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Returns the email.
	 * @return the email.
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Returns the language.
	 * @return the language.
	 */
	public LanguageCountry getLanguage() {
		return language;
	}

	/**
	 * Sets the id.
	 * @param id the id to set.
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Sets the name.
	 * @param name the name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Sets the email.
	 * @param email the email to set.
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * Sets the language.
	 * @param language the language to set.
	 */
	public void setLanguage(LanguageCountry language) {
		this.language = language;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(229, 37);
		hash.append(id);
		hash.append(name);
		hash.append(email);
		hash.append(language);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof User) {
			User compare = (User) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.id, compare.id);
			equals.append(this.name, compare.name);
			equals.append(this.email, compare.email);
			equals.append(this.language, compare.language);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(IUser compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.id, compare.getId());
		builder.append(this.name, compare.getName());
		builder.append(this.email, compare.getEmail());
		builder.append(this.language, compare.getLanguage());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		string.append("id", id);
		if (name != null) {
			string.append("name", name);
		}
		if (email != null) {
			string.append("email", email);
		}
		if (language != null) {
			string.append("language", language);
		}
		return string.toString();
	}
}
